var _Constants =
{
	ReadyState:
	{
		Uninitialized: 0,
		Loading: 1,
		Loaded:2,
		Interactive:3,
		Complete: 4
	},
		
	Status:
	{
		OK: 200,
		
		Created: 201,
		Accepted: 202,
		NoContent: 204,
		
		BadRequest: 400,
		Forbidden: 403,
		NotFound: 404,
		Gone: 410,
		
		ServerError: 500
	},
		
	Method:
	{
	    Get: "GET",
	    Post: "POST"
	},
	
	IEHttpObject: "Microsoft.XMLHTTP"
}

var _isIE = window.ActiveXObject ? true : false;

function CallBack()
{
    if (_ServerRequest.readyState == _Constants.ReadyState.Complete) {
        if (_ServerRequest.status == _Constants.Status.OK)
        {
            // alert(_ServerRequest.responseText.replace(";", ";\r\n"));
            eval(_ServerRequest.responseText);
            if(ServerRequest.CallbackDelegate) ServerRequest.CallbackDelegate();
        }
        else
        {
            alert(_ServerRequest.responseText + ", " + _ServerRequest.status);
            alert("There was a problem retrieving the Server data:\n" + _ServerRequest.statusText);
        }
    }        
}

var ServerRequest = 
{
    PrepareRequest: function(data, mthd)
    {
        var queryString = "";
        for(i=0;i<data.length;i+=2)
        {
            queryString += data[i] + "=" + data[i+1] + "&";
        }
        return queryString;
    },
    
    Execute: function(url, mthd, data, async)
    {
        if (!_isIE) _ServerRequest = new XMLHttpRequest(); else _ServerRequest = new ActiveXObject(_Constants.IEHttpObject);
        
        if (_ServerRequest)
        {
            _ServerRequest.onreadystatechange = CallBack;

            url += "?rnd=" + Math.random();

            if(mthd==_Constants.Method.Get) if(data) if(data.length > 0)
            {
                url += ServerRequest.PrepareRequest(data, mthd);
            }
            
            var useAsync = async ? async == true ? true : false : false;
            _ServerRequest.open(mthd, url, useAsync);
            
            if(mthd == _Constants.Method.Post)
            {
                _ServerRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                _ServerRequest.send(ServerRequest.PrepareRequest(data));
            }
            else
            {
                _ServerRequest.send(null);
            }
            
            if (!_isIE && !useAsync) { CallBack(); }
        }
    },
    
    CallbackDelegate    :       null
}

